﻿@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section styles{
    <style>
    </style>
}
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">登录账号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="username" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="realrname" placeholder="" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline layui-hide">
                    <label class="layui-form-label">邮箱</label>
                    <div class="layui-input-inline">
                        <input type="text" name="email" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline layui-hide">
                    <label class="layui-form-label">审核状态</label>
                    <div class="layui-input-inline">
                        <select name="userstatus">
                            <option value="">请选择审核状态</option>
                            <option value="1">已审</option>
                            <option value="0">未审</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    @{ await Html.RenderPartialAsync("_ToolbarAuthTpl", new SystemFunctionModel
        {
            MenuKey = SysMenuConstant.MENU_SYS_USER,
            FunDictionary = new Dictionary<string, string> { { "Add", "新增" }, { "Delete", "删除" } } //, { "Audit", "审核" }
        });
    }
</script>

<script type="text/html" id="user-bar">
    @{
      await  Html.RenderPartialAsync("_OperationTpl", new SystemFunctionModel
        {
            MenuKey = SysMenuConstant.MENU_SYS_USER,
            FunDictionary = new Dictionary<string, string> { { "Update", "编辑" }, { "Delete", "删除" }, { "ResetPwd", "重置密码" } }
        });
    }
</script>

<script type="text/html" id="user-enable">
    <input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="user-enable" {{ d.isenable== true ? 'checked' : '' }} />
</script>
<script type="text/html" id="user-sex">
    {{#if (d.sex == 1) { }}
    <span>男</span>
    {{# }else if(d.sex == 2){ }}
    <span>女</span>
    {{# }else if(d.sex == 0){ }}
    <span>保密</span>
    {{# } }}
</script>
<script type="text/html" id="user-createTime">
    {{layui.util.toDateString(d.createDate, 'yyyy-MM-dd')}}
</script>

<script type="text/html" id="user-userstatus">
    {{#if (d.userStatus == 1) { }}
    <span class="layui-badge">已审</span>
    {{# }else { }}
    <span class="layui-badge layui-bg-cyan">未审</span>
    {{# } }}
</script>

@section scripts{
    <script>
        layui.use(['table', 'form', 'lscom'], function () {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;
            let lscom = layui.lscom;
            let MODULE_PATH = "/Systems/User/";
            var istoolbar = $.trim($("#user-bar").html())=="" ? true : false;
            let cols = [
                [{
                    type: 'checkbox'
                },
                {
                    title: '账号',
                    field: 'userName',
                    align: 'left',
                    width: 120
                },
                {
                    title: '姓名',
                    field: 'realName',
                    align: 'left',
                    width: 120
                 },
                {
                    title: '部门名称',
                    field: 'companyName',
                    align: 'left',
                    width: 150
                },
                {
                    title: '用户类型',
                    field: 'userTypeName',
                    align: 'left',
                    width: 120
                },
                //{
                //    title: '性别',
                //    field: 'Sex',
                //    align: 'center',
                //    width: 80,
                //    templet: '#user-sex'
                //},
                {
                    title: '电话',
                    field: 'mobile',
                    align: 'left',
                    width: 150
                },
                //{
                //    title: '邮箱',
                //    field: 'Email',
                //    align: 'center'
                //},
                {
                    title: '用户状态',
                    field: 'isenable',
                    align: 'left',
                    templet: '#user-enable',
                    width: 120
                    },
                    //{
                    //    title: '审核状态',
                    //    field: 'UserStatus',
                    //    align: 'center',
                    //    templet: '#user-userstatus',
                    //    width: 120
                    //},
                {
                    title: '注册',
                    field: 'CreateDate',
                    align: 'center',
                    templet: '#user-createTime',
                    width: 100
                },
                {
                    title: '操作',
                    templet: '#user-bar',
                    align: 'center',
                    width: 240,
                    hide:istoolbar
                }
                ]
            ];

            table.render({
                elem: '#user-table',
                url: MODULE_PATH + "GetAllUser",
                page: true,
                cols: cols,
                skin: 'line',
                height: 'full-148',
                toolbar: '#user-toolbar',
                defaultToolbar: [{
                    title: '刷新',
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
            });

            table.on('tool(user-table)', function (obj) {
                var data = obj.data;
                if (obj.event === 'Delete') {
                    window.remove(data.id);
                } else if (obj.event === 'Update') {
                    window.edit(data.id);
                } else if (obj.event === "ResetPwd") {
                    window.resetPwd(data.id);
                }
            });

            table.on('toolbar(user-table)', function (obj) {
                if (obj.event === 'Add') {
                    window.add();
                } else if (obj.event === 'refresh') {
                    window.refresh();
                } else if (obj.event === 'Delete') {

                    let checkData = table.checkStatus(obj.config.id).data;
                    if (checkData.length === 0) {
                        parent.layer.msg("未选中数据", {
                            icon: 3,
                            time: 1000
                        });
                        return false;
                    }
                    var ids = checkData.map(function (d) { return d.id; });
                    window.remove(ids.join(','));
                } else if (obj.event === 'Audit') {
                    let checkData = table.checkStatus(obj.config.id).data;
                    if (checkData.length === 0) {
                        parent.layer.msg("未选中数据", {
                            icon: 3,
                            time: 1000
                        });
                        return false;
                    }
                    var ids = checkData.map(function (d) { return d.id; });
                    window.audit(ids.join(','));
                }
            });

            form.on('submit(user-query)', function (data) {
                table.reload('user-table', {
                    where: data.field
                })
                return false;
            });

            window.add = function () {
                parent.layer.open({
                    type: 2,
                    title: '新增用户',
                    shade: 0.1,
                    area: ['700px', '580px'],
                    content: MODULE_PATH + 'CreateUser',
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        parent.window['layui-layer-iframe' + index].submitForm();
                    }
                });
            }
            window.edit = function (id) {
                parent.layer.open({
                    type: 2,
                    title: '修改用户',
                    shade: 0.1,
                    area: ['700px', '580px'],
                    content: MODULE_PATH + 'EditUser' + lscom.utils.formatString("?id={0}", id),
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        parent.window['layui-layer-iframe' + index].submitForm();
                    }
                });
            }

            window.remove = function (data) {
                parent.layer.confirm('确定删除吗?', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    parent.layer.close(index);
                    let waitIndex = parent.layer.load(2);
                    lscom.ajax({
                        url: MODULE_PATH + 'DelUser',
                        data: JSON.stringify({ "ids": data }),
                        lsHandleError: false
                    }).done(function (data) {
                        if (data.code == 0) {
                            lscom.notify.success(data.message);
                            table.reload('user-table');
                        }
                    }).fail(function (message) {
                        lscom.notify.error(message);
                    }).always(function () {
                        parent.layer.close(waitIndex);
                    });
                });
            }

            window.resetPwd = function (id) {
                parent.layer.confirm('是否重置当前用户密码?', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    parent.layer.close(index);
                    let waitIndex = parent.layer.load(2);
                    lscom.ajax({
                        url: MODULE_PATH + 'ResetPwd',
                        data: JSON.stringify({ "uid": id }),
                        lsHandleError: false
                    }).done(function (data) {
                        if (data.code == 0) {
                            lscom.notify.success(data.message);
                            table.reload('user-table');
                        }
                    }).fail(function (message) {
                        lscom.notify.error(message);
                    }).always(function () {
                        parent.layer.close(waitIndex);
                    });
                });
            }
            window.audit = function (id) {
                parent.layer.confirm('是否审核当前选择用户?', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    parent.layer.close(index);

                    parent.layer.open({
                        type: 2,
                        title: '审核用户',
                        shade: 0.1,
                        area: ['900px', '650px'],
                        content: MODULE_PATH + 'AuditUser' + lscom.utils.formatString("?ids={0}", id),
                        btn: ['确定', '取消'],
                        yes: function (index, layero) {
                            parent.window['layui-layer-iframe' + index].submitForm();
                        }
                    });
                 
                });
            }
            form.on('switch(user-enable)', function (data) {
                let waitIndex = parent.layer.load(2);
                lscom.ajax({
                    url: MODULE_PATH + 'ModifyEnabled',
                    data: JSON.stringify({ "Id": data.value, "State": (data.elem.checked ? 1 : 0) }),
                    lsHandleError: false
                }).done(function (data) {
                    if (data.code == 0) {
                        lscom.notify.success(data.message);
                        table.reload('user-table');
                    }
                }).fail(function (message) {
                    lscom.notify.error(message);
                }).always(function () {
                    parent.layer.close(waitIndex);
                });
            });
            window.saveCallback = function (data) {
                parent.layer.close(data.index);
                lscom.notify.success(data.message);
                table.reload('user-table');
            }
            window.refresh = function () {
                table.reload('user-table');
            }
        });
    </script>

}

